package com.baigt.test;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class LambdaDemo {
    public static void main(String[] args) {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                // do something
            }
        });
        new Thread(()->{});
        PrintSomeThing(name->System.out.println(name),"Hello baigt");

        Doctor doctor=new Doctor("baigt","java and javascript");
        doctor.printName(name->System.out.println(name));
        System.out.println(doctor.getInterest(() -> "Hi"));
        Consumer consumer = input -> System.out.println(input);
//        doConsumer(consumer,"something input");
        doConsumer(consumer.andThen(input2->{
            System.out.println("input2");
        }),"something input");

        System.out.println(doSupplier(() -> "baigt"));
        System.out.println(doSupplier(() -> {return Integer.valueOf("10");}));
        System.out.println(doPredicate(input -> input.length() > 5, "12345"));
        System.out.println(doPredicate(((Predicate<String>) (input -> input.length() > 5))
                .and(input -> input.equalsIgnoreCase("12345")), "12345"));
        System.out.println("-------------");
        System.out.println(doFunction(input -> input.length(), "baigt"));
        System.out.println(doFunction(((Function<String, Integer>) (input -> input.length())).compose(input -> String.valueOf(input.length() * 3)), "baigt"));
        System.out.println(doFunction(((Function<String, Integer>) (input -> {
            System.out.println("notcompose:"+input);
            return Integer.valueOf(input)+1;
        })).compose(input -> {
            System.out.println("compose:"+input);
            return String.valueOf(Integer.valueOf(input)*3);
        }), "22"));

        doStaticReference(String::valueOf,123456);
        doMethodReference(String::toUpperCase,"baigt");

        Doctor doctor1=new Doctor("baigt007","java");
        Supplier<String> instance = doctor1::getInterest;
        System.out.println(instance.get());

        Supplier<String> stringInstance = String::new;


    }

    public static void PrintSomeThing(Print<String> str,String name) {
        str.printName(name);
    }

    public static void doConsumer(Consumer consumer,String input) {
        consumer.accept(input);
    }


    public static <T> T doSupplier(Supplier<T> supplier) {
        return supplier.get();
    }

    public static boolean doPredicate(Predicate<String> predicate,String string) {
        return predicate.test(string);
    }

    public static Integer doFunction(Function<String,Integer> function,String input) {
        return function.apply(input);
    }

    public static String doStaticReference(Function<Integer,String> function, Integer input) {
        return function.apply(input);
    }
    public static String doMethodReference(Function<String,String> function, String input) {
        return function.apply(input);
    }
}

interface Print<String>{
    void printName(String string);
}
interface GetSomething<String>{
    String getThing();
}

class Doctor{
    String name;
    String interest;
    public Doctor(String name, String interest) {
        this.name = name;
        this.interest = interest;
    }
    public String getStringInstance(){
        return new String(name);
    }

    public String getInterest() {
        return interest;
    }

    public String getInterest(GetSomething<String> get) {
        return get.getThing()+","+name;
    }

    public void printName(Print<String> str) {
            str.printName(name);
    }


}